package cn.biplam.back.operator.ddl.sql2md;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.BufCompTabCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * Buffer2Md表比较信息操作类
 *  <li>插入
 *  <li>更新
 *  <li>根据任务号删除
 *  <li>根据id查找
 *  <li>根据任务id查找
 * @author WangSong 2009-6-30下午03:33:54
 * @version 1.0
 */
public class OptCompareTable extends DataOperator{

	private static final String Table_Name = "COMPARE_TABLE";
	
	public OptCompareTable() throws ErrorMessage{
		super(Table_Name);
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(this.opId.equals(BufCompTabCode.Insert)){
			this._insert(sql);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(BufCompTabCode.Update)){
			this._update(sql);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(BufCompTabCode.Delete_By_Task_ID)){
			this._deleteByTaskID(sql,filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(BufCompTabCode.Get_By_ID)){
			this._selectByID(sql);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(BufCompTabCode.Get_By_Task_ID)){
			this._selectByTaskID(sql);
			return this.doing(sql);
		}else if(this.opId.equals(BufCompTabCode.Get_MaxOrid_By_Task_ID)){
			this._selectMaxOrIDByTaskId(sql);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(BufCompTabCode.Get_MaxOrid_By_Task_ID_And_ImpName)){
			this._selectByTaskIDAndImpName(sql);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(BufCompTabCode.Get_By_Task_ID_And_OperatorType)){
			this._selectByTaskIDAndOperatorType(sql);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(BufCompTabCode.Get_All_By_Task_ID)){
			this._selectByTaskID(sql);
			return this.doing(sql,Operator_Type.Select);
		}else{
			throw new ErrorMessage("OptCompareTable.java operatorID not found:"+ opId+" !");
		}
	}
	
	/**
	 * 根据ID查询
	 * @param sql
	 */
	private void _selectByID(ArrayList<String> sql){
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.ctid,t.imp_table_id,t.imp_table_name,           ");
		sb.append("        t.imp_table_cname,t.imp_table_desc,               ");
		sb.append("        t.est_table_id,t.est_table_name,t.est_table_cname,");
		sb.append("        t.theme,t.operator_type,t.orid,t.task_id,t.est_table_desc");
		sb.append("   from compare_table t                                   ");
		sb.append("  where t.ctid = '"+filter.getValue(MdStruc.ImpCompTabStruc.CTID)+"'    ");
		sql.add(sb.toString());
	}
	
	/**
	 * 根据任务号查询
	 * @param sql
	 */
	private void _selectByTaskID(ArrayList<String> sql){
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.ctid,t.imp_table_id,t.imp_table_name,             ");
		sb.append("        t.imp_table_cname,t.imp_table_desc,                 ");
		sb.append("        t.est_table_id,t.est_table_name,t.est_table_cname,  ");
		sb.append("        t.theme,t.operator_type,t.orid,t.task_id,t.est_table_desc");
		sb.append("   from compare_table t                                     ");
		sb.append("  where t.task_id = '"+filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID)+"'");
		sb.append("    and t.imp_table_id is not null ");
		sb.append("    order by t.orid");
		sql.add(sb.toString());
	}
	
	/**
	 * 根据任务号查询
	 * @param sql
	 */
	private void _selectByTaskIDAndOperatorType(ArrayList<String> sql){
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.ctid,t.imp_table_id,t.imp_table_name,             ");
		sb.append("        t.imp_table_cname,t.imp_table_desc,                 ");
		sb.append("        t.est_table_id,t.est_table_name,t.est_table_cname,  ");
		sb.append("        t.theme,t.operator_type,t.orid,t.task_id,t.est_table_desc");
		sb.append("   from compare_table t                                     ");
		sb.append("  where t.task_id = '"+filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID)+"'");
		sb.append("    and t.operator_type = '"+filter.getValue(MdStruc.ImpCompTabStruc.OPERATOR_TYPE)+"'");
		sb.append("    order by t.orid");
		sql.add(sb.toString());
	}
	
	/**
	 * 根据任务号和导入表名查询
	 * @param sql
	 */
	private void _selectByTaskIDAndImpName(ArrayList<String> sql){
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.ctid,t.imp_table_id,t.imp_table_name,             ");
		sb.append("        t.imp_table_cname,t.imp_table_desc,                 ");
		sb.append("        t.est_table_id,t.est_table_name,t.est_table_cname,  ");
		sb.append("        t.theme,t.operator_type,t.orid,t.task_id,t.est_table_desc");
		sb.append("   from compare_table t                                     ");
		sb.append("  where t.task_id = '"+filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID)+"'");
		sb.append("    and t.imp_table_name = '"+filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME)+"'");
		sql.add(sb.toString());
	}
	
	/**
	 * 取最大的排序id
	 * @param sql
	 */
	private void _selectMaxOrIDByTaskId(ArrayList<String> sql){
		StringBuilder sb = new StringBuilder();
		sb.append(" select nvl(max(t.orid)+1,0)  orid                            ");
		sb.append("   from compare_table t                                     ");
		sb.append("  where t.task_id = '"+filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID)+"'");
		sql.add(sb.toString());
	}
	
	/**
	 * 插入
	 * @param sql
	 * @throws ErrorMessage 
	 */
	private void _insert(ArrayList<String> sql) throws ErrorMessage{
		String id=generatorRowID();
		
		Insert insert=new Insert(this.tableName);
		insert.putStringFieldDenyNull(MdStruc.ImpCompTabStruc.CTID, id);
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_ID, filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_ID));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME, filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_CNAME, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_CNAME),200));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_DESC, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_DESC),2000));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_ID, filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_ID));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_NAME, filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_CNAME, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_CNAME),200));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_DESC, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_DESC),2000));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.THEME, filter.getValue(MdStruc.ImpCompTabStruc.THEME));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.OPERATOR_TYPE, filter.getValue(MdStruc.ImpCompTabStruc.OPERATOR_TYPE));
		insert.putFieldIgnoreNull(MdStruc.ImpCompTabStruc.ORID, filter.getValue(MdStruc.ImpCompTabStruc.ORID));
		insert.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.TASK_ID, filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID));
		
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 * 更新
	 * @param sql
	 * @throws ErrorMessage 
	 */
	private void _update(ArrayList<String> sql) throws ErrorMessage{
		Update update=new Update(this.tableName);
		
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_ID, filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_ID));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME, filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_CNAME, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_CNAME),200));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.IMP_TABLE_DESC, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_DESC),2000));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_ID, filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_ID));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_NAME, filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_NAME));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_CNAME, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_CNAME),200));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.EST_TABLE_DESC, TOOLS.getSubString(filter.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_DESC),2000));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.THEME, filter.getValue(MdStruc.ImpCompTabStruc.THEME));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.OPERATOR_TYPE, filter.getValue(MdStruc.ImpCompTabStruc.OPERATOR_TYPE));
		update.putFieldIgnoreNull(MdStruc.ImpCompTabStruc.ORID, filter.getValue(MdStruc.ImpCompTabStruc.ORID));
		update.putStringFieldIgnoreNull(MdStruc.ImpCompTabStruc.TASK_ID, filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID));
		update.setWhere(MdStruc.ImpCompTabStruc.CTID+"='"+filter.getValue(MdStruc.ImpCompTabStruc.CTID)+"'");
		
		sql.add(update.getSimpleSql());
	}
	
	/**
	 * 根据任务号删除
	 * @param sql
	 * @throws ErrorMessage
	 */
	public void _deleteByTaskID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		/** 删除列sql*/
		new OptCompareTableCol()._deleteByTaskID(sql, filter);
		/** 删除表sql*/
		Delete delete = new Delete(Table_Name);
		delete.setWhere(MdStruc.ImpCompTabStruc.TASK_ID+"='"+filter.getValue(MdStruc.ImpCompTabStruc.TASK_ID)+"'");
		sql.add(delete.getSimpleSql());
	}
}
